import Vue from 'vue'
import App from './App.vue'

// 使用elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import axios from 'axios'
import VueAxios from 'vue-axios'
axios.defaults.baseURL = 'http://192.168.123.144:8080';
Vue.prototype.$http=axios
Vue.use(VueAxios, axios)

import VueRouter from 'vue-router'
import router from './router'
Vue.use(VueRouter)

Vue.config.productionTip = false

// 请求拦截器
axios.interceptors.request.use(config => {
  if(config.url.indexOf('login')!==-1){
    //当请求login时，不需要token
    console.log(config.url)
  }else{
    // 将Token添加到每次请求头部
    const token = localStorage.getItem('token');
    config.headers['token'] = token;
  }
  return config;
}, error => {
  return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(response => {
  // 处理返回结果
  return response;
}, error => {
  // console.log(error)
  // console.log('拦截器响应失败')
  // console.log(error)
  // if(error.request){
  //   console.log(error.request)
  // } else if(error.response){
  //   console.log(error.response.data);
  //   console.log(error.response.status);
  // }
  if (error && error.response) {
    switch (error.response.status) {
      case 400: 
        error.message = '请求错误(400)'; 
        localStorage.clear()
        router.push('/')
        Vue.prototype.$message({
          type: 'info',
          message: 'this user is not login'
        });
        break;
      case 401: error.message = '未授权，请重新登录(401)';
        break;
      case 403: error.message = '拒绝访问(403)';
        break;
      case 404: error.message = '请求出错(404)'; 
        break;
      case 408: error.message = '请求超时(408)'; 
        break;
      case 500: error.message = '服务器错误(500)'; 
        break;
      case 501: error.message = '服务未实现(501)'; 
        break;
      case 502: error.message = '网络错误(502)'; 
        break;
      case 503: error.message = '服务不可用(503)'; 
        break;
      case 504: error.message = '网络超时(504)'; 
        break;
      case 505: error.message = 'HTTP版本不受支持(505)';
        break;
      default: error.message = '连接出错';
    }
  }else{
    error.message ='连接服务器失败!'
  }
  return Promise.reject(error);
});

new Vue({
  render: h => h(App),
  router:router
}).$mount('#app')
